
$(document).ready(function(){
    showByBrand();
    doShopCarReady();
});

//切换按品牌或者类别显示

//根据品牌显示
function showByBrand() {
    $('#all_type').removeClass("onthis");
    $('#all_brand').addClass("onthis");
    $('#brand_type').show();
    $('#goods_type').hide();

    //获取第一个品牌编号
    $("#brand_type_list li:first").addClass("onthis");
    var brand_no = $("#brand_type_list li:first").attr('brand_no');
    showThisBrandGoods(brand_no);
    console.log(brand_no);
}

//根据类型显示
function showByType() {
    $('#all_type').addClass("onthis");
    $('#all_brand').removeClass("onthis");
    $('#brand_type').hide();
    $('#goods_type').show();

    //获取第一个品牌编号
    $("#goods_type_list li:first").addClass("onthis");
    var goods_type = $("#goods_type_list li:first").attr('goods_type');
    showThisTypeGoods(goods_type);
    console.log(goods_type);
}

//品牌列表选择时间
function selectBrand(obj) {
    var brand_no = $(obj).attr('brand_no');
    $("#brand_type_list li").removeClass('onthis');
    $(obj).addClass('onthis');
    showThisBrandGoods(brand_no);
}

//品牌列表选择时间
function selectGoodType(obj) {
    var goods_type = $(obj).attr('goods_type');
    $("#goods_type_list li").removeClass('onthis');
    $(obj).addClass('onthis');
    showThisTypeGoods(goods_type);
}

//显示该品牌的商品
function showThisBrandGoods(brand_no) {
    $('.goods').hide();
    $('.'+brand_no).show();
}

//显示该品牌的商品
function showThisTypeGoods(goods_type) {
    $('.goods').hide();
    $('.'+goods_type).show();
}

//==================================购物车相关=======================================
//清空购物车
function emptyShopCar() {
    if(confirm("确认清空购物车？")){
        $.ajax({
            type: "post",
            url: '/shop/emptyShopCar.do',
            dataType: "json",
            data: {
            },
            success: function(resp){
                console.log(resp);
                if(resp.result==="success"){
                    location.reload();
                }else{
                    alertTips("系统异常！");
                }
            }
        });
    }
}


//初始化购物车显示信息
function doShopCarReady() {
    $.ajax({
        type: "post",
        url: '/shop/getShopCarInfo.do',
        dataType: "json",
        data: {
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==="success"){
                showChopCarInfo(resp.shop_car_info);
            }
        }
    });
}

//添加商品到购物车
function addToShopCar(goods_no) {
    $.ajax({
        type: "post",
        url: '/shop/addToShopCar.do',
        dataType: "json",
        data: {
            'goods_no':goods_no,
            'goods_num':1,
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==="success"){
                showChopCarInfo(resp.shop_car_info,goods_no);
            }else{
                alertTips("添加到购物车异常！")
            }
        }
    });
}

//从购物车删除商品
function subFromShopCar(goods_no) {
    $.ajax({
        type: "post",
        url: '/shop/subFromShopCar.do',
        dataType: "json",
        data: {
            'goods_no':goods_no,
            'goods_num':1
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==="success"){
                showChopCarInfo(resp.shop_car_info,goods_no);
            }else{
            }
        }
    });
}


//显示购物车信息
function showChopCarInfo(shop_car_info,goods_no) {
    var html = "";
    var a=0;
    if(shop_car_info.goods_list.length>0){
        $('#car_price').html(shop_car_info.price);
        $('#goods_num').html(shop_car_info.goods_list.length);

        for (var i=0;i<shop_car_info.goods_list.length;i++){
           if(shop_car_info.goods_list[i].goods_no === goods_no){
               a=1;
           }
            html = html+"<li>\n" +
                "<h1>"+shop_car_info.goods_list[i].goods_name+"</h1>" +
                "<span>¥"+shop_car_info.goods_list[i].goods_price+"</span>" +
                "<div class=\"increase_box\">" +
                "<div class=\"after_plus\">" +
                "<i class=\"iconfont icon-jian jian sc\" onclick=\"subFromShopCar('"+shop_car_info.goods_list[i].goods_no+"')\" ></i>" +
                "<span class=\"num\">"+shop_car_info.goods_list[i].goods_num+"</span>" +
                "</div>" +
                "<i class=\"iconfont icon-jia plus\" onclick=\"addToShopCar('"+shop_car_info.goods_list[i].goods_no+"')\" ></i>" +
                "</div>" +
                "</li>";

            // $('#'+shop_car_info.goods_list[i].goods_no+"_after_plus").css("opacity","1").css("right","15px");
            $('#'+shop_car_info.goods_list[i].goods_no+"_num").html(shop_car_info.goods_list[i].goods_num);
            $('#'+shop_car_info.goods_list[i].goods_no+"_after_plus").animate({opacity:'1', right:'15px'});
        }
    }else{
        $('#goods_num').html(0);
        $('#car_price').html(0);
    }
    if(a===0){
        $('#'+goods_no+"_after_plus").animate({opacity:'0', right:'0px'});
        $('#'+goods_no+"_num").html(0);
    }

    $('#shop_car_list').html(html);
}

//跳转到购物车
function toShoCar() {
    var goods_num = parseInt($('#goods_num').html());
    if(goods_num>0){
        location.href=ctx+'/shop/shopCar.do';
    }else{
        alertTips("请先添加商品到购物车！");
    }
}


//==================================================================================